<?php


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class AddressController extends Controller
{
    /**
     * 获取地址
     * @return \Illuminate\Http\JsonResponse
     */
    public function addrList()
    {
        $uid = \Auth::user();
        if (!$uid){
            return $this->throwError('ErrInvalidUID');
        }
        $where = ['userId' => $uid];
        $address = DB::table('addrs')->where($where)->get();
        if (!$address){
            return $this->throwError('ErrExchangeList');
        }
        return $this->result($address);
    }

    /**
     * 创建地址
     * @param Request $req
     * @return \Illuminate\Http\JsonResponse
     */
    public function createAddr(Request $req)
    {
        $input = $req->input();
        $name = $input['name'] ?? '';
        $phoneNumber = $input['phoneNumber'] ?? '';
        $address = $input['address'] ?? '';
        $detailAddr = $input['detailAddr'] ?? '';
        $isDefault = $input['isDefault'] ?? 0;
        if(!in_array($isDefault, [0,1])) $isDefault = 0;
        if (!$name || !$phoneNumber || !$address || !$detailAddr){
            return $this->throwError('ErrClientParams');
        }
        $uid = \Auth::user();
        if (!$uid){
            return $this->throwError('ErrInvalidUID');
        }
        $data = [
            'userId' => $uid,
            'name'  => $name,
            'phoneNumber' => $phoneNumber,
            'address' => $address,
            'detailAddr' => $detailAddr,
            'isDefault' => $isDefault
        ];
        $res = DB::table('addrs')->insert($data);
        if (!$res){
            return $this->throwError('ErrCreateAddr');
        }
        return $this->result();
    }

    /**
     * 修改地址
     */
    public function fixAddr(Request $req)
    {
        $input = $req->input();
        $id = $input['id'] ?? '';
        $name = $input['name'] ?? '';
        $phoneNumber = $input['phoneNumber'] ?? '';
        $address = $input['address'] ?? '';
        $detailAddr = $input['detailAddr'] ?? '';
        $isDefault = $input['isDefault'] ?? '';
        if (!$id || !$name || !$phoneNumber || !$address || !$detailAddr || !$isDefault){
            return $this->throwError('ErrClientParams');
        }
        $uid = \Auth::user();
        if (!$uid){
            return $this->throwError('ErrInvalidUID');
        }
        //设置默认地址
        if ($isDefault == 1){
            $idArr = DB::table('addrs')->where(['userId' => $uid])->pluck('id');
            DB::table('addrs')->whereIn('id',$idArr)->update([
                'isDefault' => 0
            ]);
        }
        $where = ['id' => $id];
        $user_address = DB::table('addrs')->where($where)->first();
        if (!$user_address){
            return $this->throwError('ErrFixAddr');
        }
        $res = DB::table('addrs')->where($where)->update([
            'userId' => $uid,
            'name'  => $name,
            'phoneNumber' => $phoneNumber,
            'address' => $address,
            'detailAddr' => $detailAddr,
            'isDefault' => $isDefault
        ]);
        if (!$res){
            return $this->throwError('ErrFixAddr');
        }
        return $this->result();
    }

    /**
     * 删除地址
     * @param Request $req
     */
    public function delAddr(Request $req)
    {
        $id = $req->input('id',0);
        if (!$id){
            return $this->throwError('ErrClientParams');
        }
        $uid = \Auth::user();
        if (!$uid){
            return $this->throwError('ErrInvalidUID');
        }
        $where = ['id' => $id];
        $address = DB::table('addrs')->where($where)->first();
        if (!$address){
            return $this->throwError('ErrDelAddr');
        }
        $res = DB::table('addrs')->where($where)->delete();
        if (!$res){
            return $this->throwError('ErrDelAddr');
        }
        return $this->result();
    }
}
